function * generatorFunc(){}
const g = generatorFunc();
console.dir(g);
console.dir(g.next);
console.dir(g.return);
console.dir(g.throw);
// Object [Generator] {}
// [Function: next]
// [Function: return]
// [Function: throw]

function * generatorFunc1(){
    yield * [1,2,3,4,5];
}
const obj = generatorFunc1();
console.log(obj.next());
console.log(obj.next());
console.log(obj.return(4));
console.log(obj.next());
// { value: 1, done: false }
// { value: 2, done: false }
// { value: 4, done: true }
// { value: undefined, done: true }
const obj2 = generatorFunc1();
for(let x of obj2){
    if(x === 3){
        console.log(obj2.return("迭代强制结束了"));
    }
    console.log(x);
}


function * generatorFunc2(){
    for(let x of [1,2,3,4,5,6]){
        yield x;
    }
};
const generatorObj = generatorFunc2();
for(let x of generatorObj){
    if(x === 4){
        generatorObj.return('haha');
    }
    console.log(x);
}
// 1
// 2
// 3
// 4

console.log('------------------2.throw---------------------------');
function * generatorFunc3(){
    for(let x of [7,8,9,10,11,12]){
        yield x;
    }
}
const generatorOb3 = generatorFunc3();
try{
    console.log(generatorOb3.next());
    console.log(generatorOb3.throw('thorw'));
    console.log(generatorOb3.next());
}catch(err){
    console.log('---',err);
}
// { value: 7, done: false }
// --- thorw

function * generatorFunc4(){
    for(let x of [11,22,33,44]){
        try{
            yield x;
        }catch(err){
            console.log('生成器对象内部捕获错误==>',err);
        }
    }
}
// const generatorObj4 = generatorFunc4();
// for(let x of generatorObj4){
//     if( x === 33){
//         generatorObj4.throw('hello error!')
//     }
//     console.log(x);
// }
// 11
// 22
// 生成器对象内部捕获错误==> hello error!
// 33
const generatorObj44 = generatorFunc4();
console.log(generatorObj44.next());
// console.log(generatorObj44.throw());
generatorObj44.throw()
console.log(generatorObj44.next());
console.log(generatorObj44.next());
// { value: 11, done: false }
// 生成器对象内部捕获错误==> undefined
// { value: 22, done: false }
// { value: 33, done: false }
// { value: 44, done: false }

